package com.yao.shop.controller;

import com.yao.shop.common.Page;
import com.yao.shop.common.Result;
import com.yao.shop.pojo.ShopProductCategory;
import com.yao.shop.pojo.vo.CategoryBrandVo;
import com.yao.shop.pojo.vo.ShopProductCategoryVo;
import com.yao.shop.service.ShopProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shopProductCategory")
public class ShopProductCategoryController {
    @Autowired
    private ShopProductCategoryService shopProductCategoryService;

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result<?> save(@RequestBody ShopProductCategory shopProductCategory){
        shopProductCategoryService.save(shopProductCategory);
        return new Result<>("添加成功");
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result<?> update(@RequestBody ShopProductCategory shopProductCategory){
        shopProductCategoryService.update(shopProductCategory);
        return new Result<>("修改成功");
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Result<ShopProductCategory> get(@PathVariable Long id) {
        ShopProductCategory category = shopProductCategoryService.get(id);
        return new Result<>(category);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Result<?> delete(@PathVariable Long id) {
        shopProductCategoryService.delete(id);
        return new Result<>("删除成功");
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.POST)
    public Result<Page<ShopProductCategory>> getByPage(@RequestBody Page<ShopProductCategory> page) {
        page = shopProductCategoryService.getByPage(page);
        return new Result<>(page);
    }

    /**
     * 树形列表查询
     *
     * @return
     */
    @RequestMapping(value = "/getTree", method = RequestMethod.GET)
    public Result<List<ShopProductCategoryVo>> getTree() {
        List<ShopProductCategoryVo> list = shopProductCategoryService.getTree();
        return new Result<>(list);
    }

    /**
     * 查询树形下拉框
     * @return
     */
    @RequestMapping(value = "/getSelectTree", method = RequestMethod.GET)
    public Result<List<ShopProductCategoryVo>> getSelectTree() {
        List<ShopProductCategoryVo> list = shopProductCategoryService.getSelectTree();
        return new Result<>(list);
    }
    /**
     * 获取所有的二级分类
     *
     * @return
     */
    @RequestMapping(value = "/getAllSecond", method = RequestMethod.GET)
    public Result<List<ShopProductCategory>> getAllSecond() {
        List<ShopProductCategory> list = shopProductCategoryService.getAllSecond();
        return new Result<>(list);
    }

    /**
     * 查询首页分类
     *
     * @return
     */
    @RequestMapping(value = "/getNavList", method = RequestMethod.GET)
    public Result<List<ShopProductCategory>> getNavList() {
        List<ShopProductCategory> list = shopProductCategoryService.getNavList();
        return new Result<>(list);
    }

    /**
     * 根据分类ID查询同级的分类和品牌
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "/getCategoryAndBrandById/{categoryId}", method = RequestMethod.GET)
    public Result<CategoryBrandVo> getCategoryAndBrandById(@PathVariable Long categoryId) {
        CategoryBrandVo vo = shopProductCategoryService.getCategoryAndBrandById(categoryId);
        return new Result<>(vo);
    }



}
